home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / DEARC31.ARJ / DEARC.PAS < prev    next >
Pascal/Delphi Source File  |  1988-07-26  |  10KB  |  422 lines

  1. Program Dearc;
  2. (*
  3.  DEARC.PAS - Program to extract all files from an archive created by version
  4.              5.12 or earlier of the ARC utility.
  5.  
  6.              ARC is COPYRIGHT 1985-1988 by System Enhancement Associates.
  7.              PKARC/PKXARC are Copyright 1986-1988 by PKWARE, Inc.
  8.  
  9.  
  10.     This program requires Turbo Pascal Version 4.0 or higher.
  11.  
  12.  Usage:  DEARC arcname
  13.  
  14.     arcname is the path/file name of the archive file. All files contained
  15.     in the archive will be extracted into the current directory.
  16.  
  17.  HISTORY:
  18.  
  19.    *** ORIGINAL AUTHOR UNKNOWN ***
  20.  
  21.   Version 1.01 - 10/19/85. Changed end-of-file processing to, hopefully, be
  22.                            more compatible with CPM (whatever that is).
  23.  
  24.   Version 1.01A - 12/19/85 By Roy Collins
  25.                            Mail: TechMail BBS @ 703-430-2535
  26.                                  - or -
  27.                                  P.O.Box 1192, Leesburg, Va 22075
  28.                            Modified V1.01 to work with Turbo Pascal Version 2
  29.                            Added functions ARGC (argument count) and ARGV
  30.                            (argument value)
  31.                            Modified all references to "EXIT" command to be
  32.                            GOTO EXIT, with EXIT defined as a LABEL, at the
  33.                            end of the function/procedure involved.
  34.                            Will not accept path names - archives must be in
  35.                            the current directory.
  36.  
  37.   Version 2.00 - 6/11/86   By David W. Carroll
  38.                            Mail: High Sierra RBBS-PC @ 209/296-3534
  39.                            Now supports ARC version 5.12 files, compression
  40.                            types 7 and 8.
  41.  
  42.   Version 3.00 - 7/30/87   By Richard P. Byrne
  43.                            UN*X E-Mail:  ...!ihnp4!mduxf!rpb
  44.                            BBS Mail:     Software Society BBS @ (201) 729-7410
  45.                            Modified Version 2.00 to handle compression type
  46.                            9 (ie. Squashed ).
  47.  
  48.   Version 3.10 - 7/26/88   By Paul Roub
  49.                            BBS Mail: Society BBS (407)-773-2831
  50.                                      FIDONET Programming Echo
  51.                                      FIDONET C Echo
  52.                            Compuserve EasyPlex to [71131,157]
  53.                            Modified Version 3.00:
  54.                              Ported to Turbo Pascal v4.0
  55.                              Added Time/Date stamping of extracted files
  56.                              Removed all floating point
  57.                              Added confirmation when overwriting existing file
  58.                              Display type of decompression being done
  59.                              Updated docs
  60.                              Removed CP/M style end-of-file padding (do you
  61.                                really want a bunch of Control-Z's at the
  62.                                end of a .COM file?)
  63.                              By the way,  argc and argv are gone,  and of
  64.                                COURSE you can use pathnames...
  65. *)
  66.  
  67.  
  68. (*
  69.  *  other units involved
  70.  *)
  71. uses
  72.   dearcabt,                           (* abort() routine                    *)
  73.   dearcglb,                           (* global variables,  types           *)
  74.   dearcio,                            (* input/output routines              *)
  75.   dearcunp,                           (* unPacking stuff                    *)
  76.   dearcusq,                           (* unSqueezing routines               *)
  77.   dearclzw;                           (* LZW (unCrunching and unSquashing   *)
  78.  
  79.  
  80. (**
  81.  *
  82.  *  Name:         function fn_to_str
  83.  *  Description:  convert strings from C format (trailing 0) to Turbo Pascal
  84.  *                format (leading length byte).
  85.  *  Parameters:   var -
  86.  *                  fn : fntype : filename to convert
  87.  *  Returns:      converted filename
  88.  *
  89. **)
  90. function fn_to_str(var fn : fntype) : strtype;
  91. var
  92.   s : strtype;
  93.   i : integer;
  94. begin
  95.   s := '';
  96.   i := 0;
  97.  
  98.   while fn[i] <> #0 do
  99.     begin
  100.       s := s + fn[i];
  101.       i := i + 1
  102.     end;
  103.   fn_to_str := s
  104. end; (* func fn_to_str *)
  105.  
  106.  
  107. (**
  108.  *
  109.  *  Name:         procedure GetArcName
  110.  *  Description:  get the name of the archive file
  111.  *  Parameters:   none
  112.  *
  113. **)
  114. procedure GetArcName;
  115. var
  116.   i : integer;
  117. begin
  118.   if (ParamCount > 1) then
  119.     abort('Too many parameters');
  120.  
  121.   if (ParamCount = 1) then
  122.     arcname := ParamStr(1)
  123.   else
  124.     begin
  125.       write('Enter archive filename: ');
  126.       readln(arcname);
  127.       if arcname = '' then
  128.         abort('No file name entered');
  129.       writeln;
  130.       writeln;
  131.     end;
  132.  
  133.   for i := 1 to length(arcname) do
  134.     arcname[i] := UpCase(arcname[i]);
  135.  
  136.   if pos('.', arcname) = 0 then
  137.     arcname := arcname + '.ARC'
  138. end; (* proc GetArcName *)
  139.  
  140.  
  141. (**
  142.  *
  143.  *  Name:         function readhdr
  144.  *  Description:  read a file header from the archive file
  145.  *  Parameters:   var -
  146.  *                  hdr : heads - header to read
  147.  *  Returns:      FALSE : eof found
  148.  *                TRUE  : header found
  149.  *
  150. **)
  151. function readhdr(var hdr : heads) : boolean;
  152. label
  153.   exit;
  154. var
  155.   name : fntype;
  156.   try  : integer;
  157. begin
  158.   try := 10;
  159.  
  160.   if endfile then
  161.     begin
  162.       readhdr := FALSE;
  163.       goto exit               (******** was "exit" ************)
  164.     end;
  165.  
  166.   while get_arc <> arcmarc do
  167.     begin
  168.       if try = 0 then
  169.         abort(arcname + ' is not an archive');
  170.       try := try - 1;
  171.       writeln(arcname, ' is not an archive, or is out of sync');
  172.       if endfile then
  173.         abort('Archive length error')
  174.     end; (* while *)
  175.  
  176.   hdrver := get_arc;
  177.  
  178.   if hdrver < 0 then
  179.     abort('Invalid header in archive ' + arcname);
  180.  
  181.   if hdrver = 0 then         { special end of file marker }
  182.     begin
  183.       readhdr := FALSE;
  184.       goto exit               (******** was "exit" ************)
  185.     end;
  186.  
  187.   if hdrver = 1 then
  188.     begin
  189.       fread(hdr, sizeof(heads) - sizeof(longint));
  190.       hdrver := 2;
  191.       hdr.length := hdr.size
  192.     end
  193.   else
  194.     fread(hdr, sizeof(heads));
  195.  
  196.   readhdr := TRUE;
  197.  
  198. exit:
  199.  
  200. end; (* func readhdr *)
  201.  
  202.  
  203. (**
  204.  *
  205.  *  Name:         procedure unpack
  206.  *  Description:  unpack one file
  207.  *  Parameters:   var -
  208.  *                  hdr : heads - header of file to unpack
  209.  *
  210. **)
  211. procedure unpack(var hdr : heads);
  212. label
  213.   exit;
  214. var
  215.   c : integer;
  216. begin
  217.   crcval  := 0;
  218.   size    := hdr.size;
  219.   state   := NOHIST;
  220.   FirstCh := TRUE;
  221.  
  222.   case hdrver of
  223.     1, 2 :
  224.       begin
  225.         c := getc_unp;
  226.  
  227.         while c <> -1 do
  228.           begin
  229.             putc_unp(c);
  230.             c := getc_unp
  231.           end
  232.       end;
  233.  
  234.     3    :
  235.       begin
  236.         c := getc_unp;
  237.         while c <> -1 do
  238.           begin
  239.             putc_ncr(c);
  240.             c := getc_unp
  241.           end
  242.       end;
  243.  
  244.     4    :
  245.       begin
  246.         init_usq;
  247.         c := getc_usq;
  248.  
  249.         while c <> -1 do
  250.           begin
  251.             putc_ncr(c);
  252.             c := getc_usq
  253.           end
  254.       end;
  255.  
  256.     5    :
  257.       begin
  258.         init_ucr(0);
  259.         c := getc_ucr;
  260.  
  261.         while c <> -1 do
  262.           begin
  263.             putc_unp(c);
  264.             c := getc_ucr
  265.           end
  266.       end;
  267.  
  268.     6    :
  269.       begin
  270.         init_ucr(0);
  271.         c := getc_ucr;
  272.  
  273.         while c <> -1 do
  274.           begin
  275.             putc_ncr(c);
  276.             c := getc_ucr
  277.           end
  278.       end;
  279.  
  280.     7    :
  281.       begin
  282.         init_ucr(1);
  283.         c := getc_ucr;
  284.  
  285.         while c <> -1 do
  286.           begin
  287.             putc_ncr(c);
  288.             c := getc_ucr
  289.           end
  290.       end;
  291.  
  292.     8    :
  293.       decomp(0);
  294.  
  295.     9    :
  296.       decomp(1);
  297.  
  298.     else
  299.       begin
  300.         writeln('I dont know how to unpack file ', fn_to_str(hdr.name));
  301.         writeln('I think you need a newer version of DEARC');
  302.         fseek(hdr.size, 1);
  303.         goto exit                         (******** was "exit" ************)
  304.       end
  305.   end; (* case *)
  306.  
  307.   if crcval <> hdr.crc then
  308.     writeln('WARNING: File ', fn_to_str(hdr.name), ' fails CRC check');
  309.  
  310. exit:
  311.  
  312. end; (* proc unpack *)
  313.  
  314.  
  315. (**
  316.  *
  317.  *  Name:         procedure extract_file
  318.  *  Description:  extract one file from archive
  319.  *  Parameters:   var -
  320.  *                  hdr : heads - header for file to extract
  321.  *
  322. **)
  323. procedure extract_file(var hdr : heads);
  324. var
  325.   st : strtype;
  326.   ch : char;
  327.   fil : file;
  328. begin
  329.   extname := fn_to_str(hdr.name);
  330.  
  331.   assign(fil, extname);
  332.   {$I-}
  333.   reset(fil);
  334.   {$I+}
  335.  
  336.   if (ioresult = 0) then
  337.     begin
  338.       close(fil);
  339.  
  340.       repeat
  341.         write('  File ', extname, ' exists.  Overwrite (y/n)? ');
  342.         readln(st);
  343.         ch := upcase(st[1]);
  344.       until ((ch = 'Y') or (ch = 'N'));
  345.  
  346.       if (ch = 'N') then
  347.         begin
  348.           fseek(hdr.size, 1);
  349.           writeln('  ', extname, ' skipped.');
  350.           exit;
  351.         end;
  352.     end;
  353.  
  354.   case hdrver of
  355.     1, 2    : write('Extracting ');
  356.     3       : write('unPacking  ');
  357.     4       : write('unSqueezing');
  358.     5, 6, 7 : write('uncrunching');
  359.     8       : write('unCrunching');
  360.     9       : write('unSquashing');
  361.   end;
  362.  
  363.   writeln(' : ', extname);
  364.  
  365.   open_ext;
  366.   unpack(hdr);
  367.   close_ext(hdr);
  368. end; (* proc extract *)
  369.  
  370.  
  371. (**
  372.  *
  373.  *  Name:         procedure extarc
  374.  *  Description:  extract all files from an archive
  375.  *  Parameters:   none
  376.  *
  377. **)
  378. procedure extarc;
  379. var
  380.   hdr : heads;
  381. begin
  382.   open_arc;
  383.  
  384.   while readhdr(hdr) do
  385.     extract_file(hdr);
  386.  
  387.   close_arc;
  388. end; (* proc extarc *)
  389.  
  390.  
  391. (**
  392.  *
  393.  *  Name:         procedure PrintHeading
  394.  *  Description:  print DEARC header info
  395.  *  Parameters:   none
  396.  *
  397. **)
  398. procedure PrintHeading;
  399. begin
  400.   writeln;
  401.   writeln('Turbo Pascal DEARC Utility');
  402.   writeln('Version 3.1, 7/26/88');
  403.   writeln('Supports Phil Katz "squashed" files');
  404.   writeln;
  405. end; (* proc PrintHeading *)
  406.  
  407.  
  408. (**
  409.  *
  410.  *  Name:         (main routine)
  411.  *  Description:  print header information
  412.  *                get the archive file name
  413.  *                do the extraction
  414.  *
  415. **)
  416. begin
  417.   PrintHeading;
  418.   GetArcName;   { get the archive file name }
  419.   extarc        { extract all files from the archive }
  420. end.
  421.  
  422.